﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodWorx.Core.Random
{
    /// <summary>
    /// Generate Random Strings
    /// </summary>
    public class RandomStringGenerator
    {


        public RandomCharacterGenerator RandomCharacterGenerator
        {
            get;
            private set;
        }

        public RandomStringGenerator()
            : this(new RandomCharacterGenerator())
        {
        }

        public RandomStringGenerator(RandomCharacterGenerator characterGenerator)
        {
            this.RandomCharacterGenerator = characterGenerator;
        }

        /// <summary>
        /// Returns a random string by choosing random characters from the input character
        /// array with the specified length
        /// </summary>
        /// <param name="from"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public string Next(char[] from, int length)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(this.RandomCharacterGenerator
                    .Next(from));
            }

            return sb.ToString();
        }

        /// <summary>
        /// Returns a random string by choosing random characters from the input character
        /// array with a random length the min and max length values (inclusive)
        /// </summary>
        /// <param name="from"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public string Next(char[] from, int min, int max)
        {
            int length = this.RandomCharacterGenerator
                             .RandomNumberGenerator.Next(min, max);

            return Next(from, length);
        }

        public string NextOutsideOfLengthRange(char[] from, int min, int max)
        {
            bool shorter = this.RandomCharacterGenerator.RandomNumberGenerator.NextBoolean();

            if (shorter)
                return Next(from, min - 1);
            else
                return Next(from, max + 1);
        }

        public string NextNotIn(char[] from, int min, int max)
        {
            int length = this.RandomCharacterGenerator
                            .RandomNumberGenerator.Next(min, max);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(this.RandomCharacterGenerator
                    .NextNotIn(from));
            }

            return sb.ToString();
        }
    }
}
